﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FuncBinder.cs" company="">
//   
// </copyright>
// <summary>
//   The func binder.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using System;

    using Libium.Adapters;

    /// <summary>
    /// The func binder.
    /// </summary>
    /// <typeparam name="T1">
    /// </typeparam>
    /// <typeparam name="T2">
    /// </typeparam>
    public class FuncBinder<T1, T2> : IBinder
        where T1 : class where T2 : class
    {
        #region Fields

        /// <summary>
        ///   The _create binding func.
        /// </summary>
        private readonly Func<IBindService, T1, T2, IBinding<T1, T2>> _createBindingFunc;

        #endregion

        // TODO Remove adapt services
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FuncBinder{T1,T2}"/> class.
        /// </summary>
        /// <param name="createBindingFunc">
        /// The create binding func. 
        /// </param>
        public FuncBinder(Func<IBindService, T1, T2, IBinding<T1, T2>> createBindingFunc)
        {
            this._createBindingFunc = createBindingFunc;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the type 1.
        /// </summary>
        public Type Type1
        {
            get
            {
                return typeof(T1);
            }
        }

        /// <summary>
        ///   Gets the type 2.
        /// </summary>
        public Type Type2
        {
            get
            {
                return typeof(T2);
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create binding.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        /// <returns>
        /// The <see cref="IBinding"/> . 
        /// </returns>
        public IBinding CreateBinding(IBindService service, object obj1, object obj2)
        {
            var typedObj1 = obj1 as T1 ?? service.AdaptService.Adapt<T1>(obj1);
            var typedObj2 = obj2 as T2 ?? service.AdaptService.Adapt<T2>(obj2);

            return this._createBindingFunc(service, typedObj1, typedObj2);
        }

        #endregion
    }
}